import {addItemToCart, getItemByProductId, updateCart} from "../../../src/api/api";
let nowUsername= sessionStorage.getItem("nowUsername");
export default {
  data(){
    return{
      username : "",
      productId : "",
      catId : "",
      item: {
        name: "golden dog",
        productId : "",
        catId : "",
        itemId : "",
        nowPrice : 54,
        oldPrice : 90,
        supplier : "",
        descn : "beautiful,cute,clever",
        image : "../../static/picture/cart-1.jpg"
      },
      itemList:[
      ]
    }
  },
  mounted(){
    this.productId = this.$route.query.productId; //获取从head传来的productId值
    this.catId = this.$route.query.catId;
    this.getItemList();
  },
  methods:{
    getItemList(){
      //得到展示列表
      console.log(this.productId);
      getItemByProductId(this.productId).then((res) =>{
        console.log("成功返回");
        console.log(res);
        console.log(res.data.data.length);
        this.itemList = res.data.data;
      }).catch((res) => {
            alert("错误：" + res);
      });
    },
    //添加到购物车
    addCart(itemId){
      let param = new FormData;
      param.append("increment", this.quantity);
      console.log("加入购物车");
      updateCart(nowUsername,this.itemId,param).then((res) => {
          this.getItem();
          console.log("加入购物车成功");
          console.log(res)
      });
    },
    addWish(itemId){
        console.log("添加成功");
        console.log(itemId);
        checkInCollect(nowUsername,itemId).then((res) =>{
          console.log(res);
          if(res.data.status==3){
            deleteItemfromCollect(nowUsername,itemId).then((res) =>{
              if (res.data.status == 0) {
                alert("商品已取消收藏");
                this.heartimg="static/picture/heart1.png";
              }
            });

          }else{
            addItemToCollect(nowUsername,itemId).then((res) => {
              console.log(res);
              if (res.data.status == 0) {
                alert("添加成功");
                this.heartimg="static/picture/heart2.png";
              } else if (res.data.status == 2) {
                alert("没有权限，请检查网络");
              }
            }).catch((res) => {
              alert("错误3：" + res);
            });

          }
        }).catch((res) => {
          alert("错误2：" + res);
        })
    },
    chooseItem(itemId){
      for(var i = 0;i < this.itemList.length;i++){
        if(itemId == this.itemList[i].itemId){
          this.item = this.itemList[i];
          break;
        }
      }
    }
    }
}
